package gui;

import javax.swing.*;
import javax.swing.table.*;

import database.EstateOperations;
import database.ProductOperations;
import database.RoadOperations;
import model.Estate;
import model.EstateCollection;
import model.ProductCollection;
import model.RoadCollection;

import java.awt.event.*;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

@SuppressWarnings("serial")
public class deliveryInputGUI extends JFrame implements ActionListener 
{
	private JPanel p1, p15, p2, p3,p4, p5;
	private JLabel l1,l15, l2, l3;
	private JButton back, next, add ,remove;
	private JTextField f1, f2;
	private JTable table;
	private DefaultTableModel model;
	private EstateOperations eo;
	private EstateCollection ecoll;
	private ResultSet rs;
	private Vector<Object> row;
	
	public deliveryInputGUI(EstateOperations e,EstateCollection ec)
	{
		super("Delivery Area Input Screen");
		this.setSize(840, 500);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setLayout(new FlowLayout());
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.eo=e;
		this.ecoll=ec;
		eo.dropEstateTable();
		eo.CreateEstateTable();
		
		//Panel 1 
		p1 = new JPanel();
		p1.setPreferredSize(new Dimension(800, 40));
		l1 = new JLabel("Enter all your Estates here");
		p1.add(l1);
		
		//Panel 1.5 
		p15 = new JPanel();
		p15.setPreferredSize(new Dimension(800, 40));
		l15 = new JLabel("Estates");
		l15.setFont(new Font("serif",Font.BOLD,26));
		p15.add(l15);
		
		
		//Panel 2
		p2 = new JPanel();
		p2.setLayout(new GridLayout(1,4));
		p2.setPreferredSize(new Dimension(600, 20));
		l2 = new JLabel("Estate Name: ");
		l3 = new JLabel("Delivery Charge: ");
		f1 = new JTextField(25);
		f1.addActionListener(this);
		f2 = new JTextField(25);
		f2.addActionListener(this);
		
		p2.add(l2);
		p2.add(f1);
		p2.add(l3);
		p2.add(f2);
		
		//Panel 3
		p3 = new JPanel();
		p3.setPreferredSize(new Dimension(800, 40));
		add = new JButton("ADD");
		add.addActionListener(this);
		remove = new JButton("REMOVE");
		remove.addActionListener(this);
		p3.add(add);
		p3.add(Box.createRigidArea(new Dimension(200,0)));
		p3.add(remove);
		
		//Panel 4
		p4 = new JPanel();
		p4.setPreferredSize(new Dimension(800, 200));
		
		// create blank JTable on screen
		String[] cols={"ID","ESTATE NAME","CHARGE"};
		int numRows=100;
		model=new DefaultTableModel(numRows,cols.length);
		model.setColumnIdentifiers(cols);
		table = new JTable(model);
		table.setFillsViewportHeight(true);
		table.setAutoCreateColumnsFromModel(false);
		JScrollPane sp=new JScrollPane(table);
		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		sp.setPreferredSize(new Dimension(700, 190));
		
		//adjust size of columns on table
		TableColumnModel tbm=table.getColumnModel();
		tbm.getColumn(0).setMaxWidth(30);
		tbm.getColumn(2).setMaxWidth(100);
		model.setRowCount(0);
		p4.add(sp,BorderLayout.CENTER);
		
		//Panel 5
		p5 = new JPanel();
		p5.setPreferredSize(new Dimension(800, 32));
		back = new JButton("BACK");
		next = new JButton("NEXT");
		back = new JButton("BACK");
		back.addActionListener(this);
		next = new JButton("NEXT");
		next.addActionListener(this);
		p5.add(back);
		p5.add(Box.createRigidArea(new Dimension(650,0)));
		p5.add(next);
		
		//Adding Panels to the page
		this.add(p1, BorderLayout.PAGE_START);
		this.add(p15, BorderLayout.NORTH);
		this.add(p2, BorderLayout.NORTH);
		this.add(p3, BorderLayout.CENTER);
		this.add(p4, BorderLayout.SOUTH);
		this.add(p5, BorderLayout.PAGE_END);
	}
	
	
	public static void main(String args[]) 
	{
		EstateOperations eo=new EstateOperations();
		EstateCollection ec=new EstateCollection(eo);
		deliveryInputGUI di = new deliveryInputGUI(eo,ec);
		di.setVisible(true);
	}
	
	//add estate to database and array
	public void addEstate()
	{
		try
		{
			if(f1.getText().equals("")||f2.getText().equals(""))
			{
				JOptionPane.showMessageDialog(null, "You must enter data in the name and charge fields","ERROR",JOptionPane.WARNING_MESSAGE);
			}
			else
			{
				String name=f1.getText();
				double price=Double.parseDouble(f2.getText());
				Estate e = new Estate(name,price);
				eo.addEstate(e);
				ecoll.addEstate();
				f1.setText("");
				f2.setText("");
			}
		}
		catch(NumberFormatException e)
		{
			JOptionPane.showMessageDialog(null, "You have not entered a correct charge price","ERROR",JOptionPane.WARNING_MESSAGE);
		}
	}

	//remove estate from database and array
		public void removeEstate()
		{
			String a;
			a=JOptionPane.showInputDialog(null, "Enter the ID of the estate you wish to remove","Remove Estate",JOptionPane.QUESTION_MESSAGE);
			if(a==null)
			{
				//do nothing
			}
			else
			{
				int b=Integer.parseInt(a);
				boolean ans=ecoll.removeEstate(b);
				if(ans==true)
				{
					JOptionPane.showMessageDialog(null, "The estate has ben deleted","Deleted",JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					JOptionPane.showMessageDialog(null, "The estate you choose is not a valid estate","Not Deleted",JOptionPane.WARNING_MESSAGE);
				}
			}
		}
		
		// update the table after every new estate added or deleted
		public void updateTable()
		{
			rs=eo.getEstate();
			int counter=0;
			model.setRowCount(0);
			try {
				while(rs.next())
				{
					row=new Vector<Object>();
					row.add(rs.getInt(1));
					row.add(rs.getString(2));
					row.add(rs.getDouble(3));
					model.insertRow(counter,row);
					counter++;
				}
			} catch (SQLException e) 
			{
				e.printStackTrace();
			}
			
		}


	@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==add)
		{
			addEstate();
			updateTable();
		}
		if(ae.getSource()==remove)
		{
			removeEstate();
			updateTable();
		}

		if(ae.getSource()==next)
		{
			RoadOperations ro=new RoadOperations();
			RoadCollection rc=new RoadCollection(ro);
			roadInputGUI rig=new roadInputGUI(ro,rc,eo);
			rig.setVisible(true);
			this.setVisible(false);
		}
		if(ae.getSource()==back)
		{
			eo.closeDB();
			ProductOperations po=new ProductOperations();
			ProductCollection p = new ProductCollection(po);
			productInputGUI pig=new productInputGUI(p,po);
			pig.setVisible(true);
			this.setVisible(false);
		}
		
	}

}
